package com.next

class Note
{
    String subject
    String text

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    User createBy
    User modifyBy

    User user

    Activity activity
    Account account
    Contact contact
    Campaign campaign
    Opportunity opportunity
    Asset asset

    static constraints = {
        subject maxSize: 32
        text type: "text"

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        user nullable: true, blank: true

        activity nullable: true, blank: true
        account nullable: true, blank: true
        contact nullable: true, blank: true
        campaign nullable: true, blank: true
        opportunity nullable: true, blank: true
        asset nullable: true, blank: true
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
        if (!user)
        {
            user = createBy
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
